/**
 * 11选5的共用逻辑
 */

// 标题
initGame(game, function() {
    fill_zonghe_row();
    fill_table();
    fill_lianma();
    fill_zhixuan();
});

// 填充总和
function fill_zonghe_row() {
    var types = ["大", "单", "尾大", "龙", '小', '双', '尾小', '虎'];
    var table = $(".zonghe");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i = 0; i < types.length; i++) {
        var key = types[i];
        var name = types[i];
        if (key == '大' || key == '小' || key == '单' || key == '双') {
            key = '和' + key;
            name = '总和' + name;
        }
        if (key == '尾大' || key == '尾小') name = '总和' + name;

        add_bet_item(key, name, tr);
        if (i == 3) {
            table.append(tr);
            tr = $("<tr></tr>");
        }
    }
    table.append(tr);
}

// 填充投注区域表格
function fill_table() {
    var ths = ["第一球", "第二球", "第三球", "第四球", "第五球"];
    var arr = ['一', '二', '三', '四', '五'];
    var types = ["大", "小", "单", "双"];
    
    // 1~5球的大小单双
    var div = $(".five");
    div.empty();
    for (var i in arr) {
        var table = $('<table class="container"></table>');
        table.append('<tr><th>第' + arr[i] + '球</th></tr>');
        for (var j in types) {
            var tr = $("<tr></tr>");
            add_bet_item(arr[i] + types[j], types[j], tr);
            table.append(tr);
        }

        var li = $("<li></li>");
        li.append(table);
        div.append(li);
    }

    // 一中一
    var table = $(".zhong1 tbody");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i = 1; i <= 15; i++) {
        if (i > 11) {
            tr.append("<td> </td>");
            continue;
        }
        var k = i < 10 ? "0" + i : i;
        add_bet_item2("任1:" + i, '<img src="../public/all/images/' + k + '.png">', tr);
        if (i % 5 == 0) {
            table.append(tr);
            tr = $("<tr></tr>");
        }
    }
    table.append(tr);

    // 1~5球的数字
    var div = $(".five2");
    div.empty();
    for (var i in arr) {
        var table = $('<table class="container"></table>');
        table.append('<tr><th>第' + arr[i] + '球</th></tr>');
        for (var j = 1; j <= 11; j++) {
            var tr = $("<tr></tr>");
            var k = j < 10 ? "0" + j : j;
            add_bet_item(arr[i] + k, '<img src="../public/all/images/' + k + '.png">', tr);
            table.append(tr);
        }

        var li = $("<li></li>");
        li.append(table);
        div.append(li);
    }
}

/*************处理连码 */
// 填充连码
function fill_lianma() {
    var types = ['任2', '任3', '任4', '任5', '任6', '任7', '任8', '组2', '组3'];
    var table = $(".lianma thead");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i in types) {
        if (i == 0)
            tr.append('<th><label class="am-radio"><input type="radio" name="radio" value="' + i + '" data-am-ucheck checked></label></th>');
        else
            tr.append('<th><label class="am-radio"><input type="radio" name="radio" value="' + i + '" data-am-ucheck></label></th>');
    }
    table.append(tr);

    var tr = $("<tr></tr>");
    for (var i in types) {
        tr.append('<td>' + gameRule.getTargetName(types[i]) + "</td>");
    }
    table.append(tr);

    var tr = $("<tr></tr>");
    for (var i in types) {
        var key = types[i];
        var rate = gameRule.map[key] ? gameRule.map[key].rate : "--";
        tr.append('<td class="special-rate" key="' + key + '">' + rate + "</td>");
    }
    table.append(tr);
    $($(".lianma input")[0]).uCheck("check");

    // 填充11个号码
    table = $(".numbers thead");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i = 0; i < 6; i++) {
        tr.append("<th>号</th>");
        tr.append("<th>勾选</th>");
    }
    table.append(tr);

    table = $(".numbers tbody");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i = 1; i <= 12; i++) {
        var k = i < 10 ? "0" + i : i;
        if (i > 11) {
            tr.append("<td> </td>");
            tr.append("<td> </td>");
            table.append(tr);
            continue;
        }
        tr.append("<td class='img'><img src='../public/all/images/" + k + ".png'></td>");
        tr.append('<td><label class="am-checkbox"><input type="checkbox" key="' + i + '" data-am-ucheck></label></td>');
        if (i == 6) {
            table.append(tr);
            tr = $("<tr></tr>");
        }
    }

    register_lianma_event();
}

// 连码的事件处理
function register_lianma_event() {
    var types = ['任2', '任3', '任4', '任5', '任6', '任7', '任8', '组2', '组3'];

    $(".lianma input").change(function(e) {
        clear_lianma();
    });
    $(".numbers input").change(function(e) {
        refresh_lianma_numbers();
    });
}

// 当前的投注类型
function get_lianma_target() {
    var types = ['任2', '任3', '任4', '任5', '任6', '任7', '任8', '组2', '组3'];
    var i = $(".lianma input:checked").val();
    if (i == '') {
        $($(".lianma input")[0]).uCheck("check");
        i = 0;
    }
    return types[parseInt(i)];
}

// 取得当前选中的数字列表
function get_lianma_numbers() {
    var arr = $(".numbers input");
    var list = [];
    for (var i = 0; i < arr.length; i++) {
        if (!arr[i].checked) continue;
        list.push($(arr[i]).attr("key"))
    }
    return list;
}

// 清理选中的数字
function clear_lianma() {
    $(".numbers input").uCheck('enable');
    $(".numbers input").uCheck('uncheck');
}

// 刷新连码的数字信息
function refresh_lianma_numbers() {
    var arr = $(".numbers input");
    var max = parseInt(get_lianma_target()[1]);
    var list = get_lianma_numbers();
    var k = get_lianma_target();
    if (k == '组2' || k == '组3') max = 5;

    arr.uCheck('enable');
    if (list.length >= max) {
        // 其他数字需要置灰
        for (var i = 0; i < arr.length; i++) {
            if (!arr[i].checked)
                $(arr[i]).uCheck('disable');
        }
    }

    for (var i = 0; i < arr.length; i++) {
        if (arr[i].checked) {
            $(arr[i]).parent().parent().attr("class", "selected");
        }
        else {
            $(arr[i]).parent().parent().attr("class", "");
        }
    }
}

// 确定投注的处理
function lianma_do_bet(v) {
    var max = parseInt(get_lianma_target()[1]);
    var list = get_lianma_numbers();
    if (v < 1 || list.length < max) {
        alert2("下注内容不正确，请重新下注！", true);
        return;
    }

    var key = get_lianma_target();
    key = key + ":" + list.join(",");
    var data = {};
    data[key] = v * 100;
    show_bet_confirm_list(data);
}
function lianma_reset() {
    clear_lianma();
}

window.parse_target_name = function(k) {
    if (k.startsWith('组2')) {
        var arr = k.split(':');
        var numbers = arr[1].split(',');
        var i = 1;
        if (numbers.length == 3) i = 3;
        if (numbers.length == 4) i = 6;
        if (numbers.length == 5) i = 10;
        return '前二组选 ' + arr[1] + ' [' + i + '组]';
    }
    if (k.startsWith('组3')) {
        var arr = k.split(':');
        var numbers = arr[1].split(',');
        var i = 1;
        if (numbers.length == 4) i = 4;
        if (numbers.length == 5) i = 10;
        return '前三组选 ' + arr[1] + ' [' + i + '组]';
    }
    return gameRule.getTargetName(k);
}
window.calc_bet_list = function(data) {
    var total = 0, count = 0;
    for (var k in data) {
        if (k.startsWith('组2')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var i = 1;
            if (numbers.length == 3) i = 3;
            if (numbers.length == 4) i = 6;
            if (numbers.length == 5) i = 10;
            count += i;
            total += data[k] * i;
            continue;
        }
        if (k.startsWith('组3')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var i = 1;
            if (numbers.length == 4) i = 4;
            if (numbers.length == 5) i = 10;
            count += i;
            total += data[k] * i;
            continue;
        }
        count++;
        total += data[k];
    }
    return [count, total];
}
window.unpack_bet_list = function(data) {
    // 将组2和组3展开
    var r = {};
    for (var k in data) {
        if (k.startsWith('组2')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var list = group2(numbers);
            for (var i in list) {
                r['组2:' + list[i].join(',')] = data[k];
            }
            r.group = '前二组选 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
        }
        else if (k.startsWith('组3')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var list = group3(numbers);
            for (var i in list) {
                r['组3:' + list[i].join(',')] = data[k];
            }
            r.group = '前三组选 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
        }
        else
            r[k] = data[k];
    }
    return r;
}

/*************处理直选 */
// 填充直选
function fill_zhixuan() {
    var types = ['直2', '直3'];
    var table = $(".zhixuan thead");
    table.empty();
    var tr = $("<tr></tr>");
    for (var i in types) {
        tr.append('<th><label class="am-radio"><input type="radio" name="radio" value="' + i + '" data-am-ucheck></label></th>');
    }
    table.append(tr);

    var tr = $("<tr></tr>");
    for (var i in types) {
        tr.append('<td>' + gameRule.getTargetName(types[i]) + "</td>");
    }
    table.append(tr);

    var tr = $("<tr></tr>");
    for (var i in types) {
        var key = types[i];
        var rate = gameRule.map[key] ? gameRule.map[key].rate : "--";
        tr.append('<td class="special-rate" key="' + key + '">' + rate + "</td>");
    }
    table.append(tr);
    $($(".zhixuan input")[0]).uCheck("check");

    // 填充11个号码
    var tables = [$(".numbers1 tbody"), $(".numbers2 tbody"), $(".numbers3 tbody")];
    for (var z in tables) {
        var table = tables[z];
        table.empty();
        var tr = $("<tr></tr>");
        for (var i = 1; i <= 12; i++) {
            var k = i < 10 ? "0" + i : i;
            if (i > 11) {
                tr.append("<td> </td>");
                tr.append("<td> </td>");
                table.append(tr);
                continue;
            }
            tr.append("<td class='img'><img src='../public/all/images/" + k + ".png'></td>");
            tr.append('<td><label class="am-checkbox"><input type="checkbox" key="' + i + '" data-am-ucheck></label></td>');
            if (i == 6) {
                table.append(tr);
                tr = $("<tr></tr>");
            }
        }
    }

    register_zhixuan_event();
}

// 直选的事件处理
function register_zhixuan_event() {
    var types = ['直2', '直3'];

    $(".zhixuan input").change(function(e) {
        clear_zhixuan();
    });
    $(".numbers1 input").change(function(e) {
        refresh_zhixuan_numbers();
    });
    $(".numbers2 input").change(function(e) {
        refresh_zhixuan_numbers();
    });
    $(".numbers3 input").change(function(e) {
        refresh_zhixuan_numbers();
    });
}

// 当前的投注类型
function get_zhixuan_target() {
    var types = ['直2', '直3'];
    var i = $(".zhixuan input:checked").val();
    return types[parseInt(i)];
}

// 取得当前选中的数字列表
function get_zhixuan_numbers(list1, list2, list3) {
    var arr = $(".numbers1 input");
    for (var i = 0; i < arr.length; i++) {
        if (!arr[i].checked) continue;
        list1.push($(arr[i]).attr("key"))
    }

    var arr = $(".numbers2 input");
    for (var i = 0; i < arr.length; i++) {
        if (!arr[i].checked) continue;
        list2.push($(arr[i]).attr("key"))
    }

    var arr = $(".numbers3 input");
    for (var i = 0; i < arr.length; i++) {
        if (!arr[i].checked) continue;
        list3.push($(arr[i]).attr("key"))
    }
}

// 清理选中的数字
function clear_zhixuan() {
    $(".numbers1 input").uCheck('enable');
    $(".numbers1 input").uCheck('uncheck');
    $(".numbers2 input").uCheck('enable');
    $(".numbers2 input").uCheck('uncheck');
    $(".numbers3 input").uCheck('enable');
    $(".numbers3 input").uCheck('uncheck');
}

// 刷新直选的数字信息
function refresh_zhixuan_numbers() {
    var key = get_zhixuan_target();
    var arr1 = $(".numbers1 input");
    var arr2 = $(".numbers2 input");
    var arr3 = $(".numbers3 input");
    var list1 = [], list2 = [], list3 = [];
    get_zhixuan_numbers(list1, list2, list3);
    
    arr1.uCheck('enable');
    arr2.uCheck('enable');
    arr3.uCheck('enable');
    var f = function(arr, list2, list3, three) {
        for (var i = 0; i < arr.length; i++) {
            var v = $(arr[i]).attr("key");
            if (list2.indexOf(v) != -1) $(arr[i]).uCheck('disable');
            if (three && list3.indexOf(v) != -1) $(arr[i]).uCheck('disable');
        }
    };
    f(arr1, list2, list3, key == '直3');
    f(arr2, list1, list3, key == '直3');
    if (key == '直3')
        f(arr3, list1, list2, true);

    var f = function(arr) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].checked) {
                $(arr[i]).parent().parent().attr("class", "selected");
            }
            else {
                $(arr[i]).parent().parent().attr("class", "");
            }
        }
    }
    f(arr1);
    f(arr2);

    if (key == '直3')
        $(".numbers3").show();
    else
        $(".numbers3").hide();
}

// 确定投注的处理
function zhixuan_do_bet(v) {
    var key = get_zhixuan_target();
    var max = parseInt(key[1]);
    var list1 = [], list2 = [], list3 = [];
    get_zhixuan_numbers(list1, list2, list3);
    if (key == '直2' && (list1.length < 1 || list2.length < 1) ||
        key == '直3' && (list1.length < 1 || list2.length < 1 || list3.length < 1)) {
        alert2("下注内容不正确，请重新下注！", true);
        return;
    }

    var data = {};
    if (key == '直2') {
        for (var i in list1) {
            var n1 = list1[i];
            for (var j in list2) {
                var n2 = list2[j];
                data['直2:' + n1 + ',' + n2] = v * 100;
            }
        }
    }
    else {
        for (var i in list1) {
            var n1 = list1[i];
            for (var j in list2) {
                var n2 = list2[j];
                for (var k in list3) {
                    var n3 = list3[k];
                    data['直3:' + n1 + ',' + n2 + ',' + n3] = v * 100;
                }
                
            }
        }
    }

    show_bet_confirm_list(data);
}
function zhixuan_reset() {
    clear_zhixuan();
}

// 检测能否下注，看下单号1-10是否满足要求
window.customCheckBetList = function(type, issue, data) {
    if (type != 'gd115' && type != 'zy115') return true;

    var limit = Util.getNumberLimit(type);
    if (limit <= 0 || limit >= 10) return true;

    var arr1 = ['一', '二', '三', '四', '五'];
    var arr2 = ['01', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11'];
    var m = {};
    for (var k in data) {
        if (k.length != 3) continue;
        var k1 = k[0], k2 = k.substring(1);
        if (arr1.indexOf(k1) != -1 && arr2.indexOf(k2) != -1) {
            if (!m[k1]) m[k1] = {};
            m[k1][k2] = true;
        }
    }
    if (Util.isEmpty(m)) return true;

    // 获取下已经下注的单据
    var bets = context.bets[type];
    if (bets) bets = bets[issue] || [];
    else bets = [];
    for (var i in bets) {
        var k = bets[i].target;
        if (k.length != 3) continue;
        var k1 = k[0], k2 = k.substring(1);
        if (arr1.indexOf(k1) != -1 && arr2.indexOf(k2) != -1) {
            if (!m[k1]) m[k1] = {};
            m[k1][k2] = true;
        }
    }

    var total = 0;

    for (var k in m) {
        var m2 = m[k];
        if (Object.keys(m2).length > limit) {
            var l = "下注失败，__g__ 定位玩法，单个名次不允许下注__v__码或者__v__以上！如有疑问，请咨询客服！";
            l = l.replace(/__g__/, get_game_display_name(type));
            l = l.replace(/__v__/g, limit);
            alert2(l, true);
            return false;
        }
    }

    return true;
}